package com.ybzl.system.mdc;

import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.ConsoleAppender;

import java.util.Map;

/***
 *  实现日志输出一次，序号增加1
 */
public class MdcConsoleAppender extends ConsoleAppender<LoggingEvent> {

    /**
     * @param event 被编码后即将输出的值
     */
    @Override
    protected void subAppend(LoggingEvent event) {
        Map<String,String> mdc = event.getMDCPropertyMap();
       if (null != mdc.get("bid")){
           String bid = mdc.get("bid");
           String[] tt = bid.split("@");
           StringBuilder sb = new StringBuilder();
           sb.append(tt[0]);
           sb.append("@");
           sb.append(Integer.parseInt(tt[1])+1);
           event.getMDCPropertyMap().put("bid",sb.toString());
        }
        super.subAppend(event);
    }
}
